package com.lanou.tipsy.tools.popupWindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.lanou.tipsy.R;

/**
 * Created by wangshuai on 15/10/30.
 * <p>
 * 自定义pipupWindow 类
 */
public class SelectPicPopupWindow extends PopupWindow {
    //声明点击按钮组件
    private Button HKTBtn;
    private Button EUSBtn;
    private Button japanBtn;
    private Button koreaBtn;
    private Button scenceBtn;
    private Button chinaBtn;
    //全部mv按钮
    private Button allMvBtn;
    //声明取消按钮组件
    private Button cancelBtn;
    //声明布局
    private View mMenuView;
    private Activity mContext;

    public SelectPicPopupWindow(Activity context, View.OnClickListener itemsOnClick) {
        super(context);
        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //绑定布局
        mMenuView = inflater.inflate(R.layout.popup_window_view, null);
        //绑定控件布局
        HKTBtn = (Button) mMenuView.findViewById(R.id.HKTBtn);
        EUSBtn = (Button) mMenuView.findViewById(R.id.EUSBtn);
        japanBtn = (Button) mMenuView.findViewById(R.id.japanBtn);
        koreaBtn = (Button) mMenuView.findViewById(R.id.koreaBtn);
        scenceBtn = (Button) mMenuView.findViewById(R.id.scenceBtn);
        chinaBtn = (Button) mMenuView.findViewById(R.id.chinaBtn);

        allMvBtn = (Button) mMenuView.findViewById(R.id.allMvBtn);
        HKTBtn = (Button) mMenuView.findViewById(R.id.HKTBtn);
        //添加监听
        HKTBtn.setOnClickListener(itemsOnClick);
        EUSBtn.setOnClickListener(itemsOnClick);
        japanBtn.setOnClickListener(itemsOnClick);
        koreaBtn.setOnClickListener(itemsOnClick);
        scenceBtn.setOnClickListener(itemsOnClick);
        chinaBtn.setOnClickListener(itemsOnClick);
        allMvBtn.setOnClickListener(itemsOnClick);
        cancelBtn = (Button) mMenuView.findViewById(R.id.cancelBtn);
        //设置按钮监听
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        //   this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //产生背景变暗效果
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.4f;
        mContext.getWindow().setAttributes(lp);
//取消按钮匿名监听
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dismiss();
            }
        });

        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        update();

                        dismiss();
                    }
                }
                return true;
            }
        });


    }
}